<template>
  <div class="page-Body">
    <h2 class="page-title">超级群</h2>
    <!-- 表单 -->
    <el-form :inline="true" :model="formInline">
      <el-form-item label="群名称">
        <el-input
          v-model="formInline.name"
          placeholder="请输入群名称"
          clearable
          style="width: 170px"
          v-noSpace
        />
      </el-form-item>
      <el-form-item label="类型">
        <el-select
          v-model="formInline.type"
          clearable
          placeholder="请选择类型"
          style="width: 170px"
        >
          <el-option
            v-for="item in ticketTypeList || []"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="申请时间">
        <TimeFormatter
          @TimeChild="TimeHandle"
          ref="childComponentRef"
        ></TimeFormatter>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="Search">搜索</el-button>
        <el-button @click="Reset">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格 -->
    <el-table
      :data="tableData"
      border
      stripe
      fit
      empty-text="暂无数据"
      show-header
      v-loading="loading"
      size="default"
      element-loading-text="Loading..."
      :header-cell-style="{ background: '#e7e1fb' }"
      style="width: 100%"
      max-height="600px"
    >
      <el-table-column type="index" label="序号" width="80" align="center" />
      <el-table-column prop="name" label="群名称" />
      <el-table-column prop="type" label="类型" />
      <el-table-column prop="creatorName" label="创建人" />
      <el-table-column prop="content" label="详细介绍" />
      <el-table-column prop="applyTime" label="申请时间" />
      <el-table-column prop="expirationTime" label="到期时间" />
    </el-table>
    <!-- 分页器 -->
    <div class="pagination-block">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[20, 40, 80, 100]"
        background
        layout="total,sizes, prev, pager, next,jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup>
import { updateCommunitySuper } from '@/api/Association/ment';
import { ref, onMounted } from 'vue';
import { useRoute } from 'vue-router';
import TimeFormatter from '@/components/permission/TimeFormatter.vue';

const route = useRoute();
// 分页变量
const currentPage = ref(1);
const pageSize = ref(20);
const total = ref(0);
// 表单数据
const formInline = ref({
  name: '',
  type: '',
});
const ticketTypeList = ref([
  { name: '行业', id: '0' },
  { name: '兴趣', id: '1' },
]);
// 表单-时间选择器
const childComponentRef = ref(null);
const TimeArray = ref([]);
// 列表数据+表单校验
const tableData = ref([]);
const loading = ref(true);
// 分页逻辑
const handleSizeChange = async (val) => {
  pageSize.value = val;
  CommunitySuper();
};
const handleCurrentChange = (val) => {
  currentPage.value = val;
  CommunitySuper();
};

// 获取列表
const CommunitySuper = async () => {
  loading.value = true;
  const params = {
    current: currentPage.value,
    size: pageSize.value,
    query: { ...formInline.value, communityId: route.params.id },
  };

  // 只有当时间数组有值时才添加时间参数
  if (TimeArray.value && TimeArray.value.length > 0) {
    params.startDate = TimeArray.value[0];
    params.endDate = TimeArray.value[1];
  }

  const res = await updateCommunitySuper(params);
  if (res.data.code === 200) {
    tableData.value = res.data.data.items;
    total.value = res.data.data.counts || 0;
    loading.value = false;
  }
};
// 搜索校验
const hasSearchConditions = () => {
  const { name, type } = formInline.value;
  return name || type || (TimeArray.value && TimeArray.value.length > 0);
};
// 搜索
const Search = async () => {
  if (!hasSearchConditions()) {
    // eslint-disable-next-line no-undef
    ElMessage({
      message: '请至少输入一个搜索条件',
      type: 'warning',
    });
    return;
  }
  CommunitySuper();
};
// 重置
const Reset = () => {
  // 如果当前已经是空值状态，直接返回
  if (
    formInline.value.name === '' &&
    formInline.value.type === '' &&
    TimeArray.value.length === 0
  ) {
    return;
  }
  formInline.value = {};

  TimeArray.value = []; // 确保清空时间数组
  if (childComponentRef.value) {
    childComponentRef.value.clear();
  }
  CommunitySuper();
};
//获取时间
const TimeHandle = (data) => {
  TimeArray.value = data;
};
onMounted(() => {
  CommunitySuper();
});
</script>

<style lang="scss" scoped></style>
